package cn.kgm.generator;

import cn.hutool.core.text.StrPool;
import cn.kgm.core.utils.ip.IpUtils;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

import javax.annotation.PostConstruct;

@Slf4j
@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@MapperScan("cn.kgm.generator.dao")
public class KapiGeneratorApplication {

	@Value("${server.port}")
	private String part;

	public static void main(String[] args) {
        SpringApplication app = new SpringApplication(KapiGeneratorApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
        printMemoryUsage();
    }

    @PostConstruct
    private void print() {
        log.info("【{}】", "请访问:");
        log.info("【{}】", "http://".concat(IpUtils.getHostIp()).concat(StrPool.COLON).concat(part));
    }

    /**
     * 输出项目占用内存信息
     */
    private static void printMemoryUsage() {
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        System.out.println("Max Memory: " + formatMemory(maxMemory));
        System.out.println("Total Memory: " + formatMemory(totalMemory));
        System.out.println("Free Memory: " + formatMemory(freeMemory));
    }

    private static String formatMemory(long bytes) {
        final double kilo = 1024.0;
        double size = (bytes / kilo) / kilo;
        return String.format("%.2f MB", size);
    }

}
